package pl.edu.wsinf.lukaszwalczak.emailclient;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.task.TaskExecutor;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.orm.jpa.JpaTransactionManager;
import org.springframework.orm.jpa.LocalEntityManagerFactoryBean;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;

/*
 * To jest klasa odpowiadająca za konfigurację Spring Framework.
 * Adnotacja @Configuration informuje Spring że są tu definicje @Bean'ów
 * Właśnie stąd Spring pobiera klase gdy chce ją wstrzyknąć poprzez @Inject
 */
@Configuration
@ComponentScan(basePackages = {"pl.edu.wsinf.lukaszwalczak.emailclient.controller",
    "pl.edu.wsinf.lukaszwalczak.emailclient.view",
    "pl.edu.wsinf.lukaszwalczak.emailclient.service"})
@EnableTransactionManagement
@EnableJpaRepositories("pl.edu.wsinf.lukaszwalczak.emailclient.repository") //http://static.springsource.org/spring-data/jpa/docs/current/reference/html/#repositories.create-instances.java-config
public class EmailClientSpringConfig {

    @Bean
    public LocalEntityManagerFactoryBean entityManagerFactory() {
        LocalEntityManagerFactoryBean factory = new LocalEntityManagerFactoryBean();
        factory.setPersistenceUnitName("EmailClientPU");
        return factory;
    }

    @Bean
    public PlatformTransactionManager transactionManager() {
        JpaTransactionManager transactionManager = new JpaTransactionManager();
        transactionManager.setEntityManagerFactory(entityManagerFactory().getObject());
        return transactionManager;
    }

    @Bean
    public TaskExecutor taskExecutor() {
        ThreadPoolTaskExecutor taskExecutor = new ThreadPoolTaskExecutor();
        return taskExecutor;
    }
}
